const tagsView = {
    state: {
      visitedViews: [],
      cachedViews: []
    },
    mutations: {
      ADD_VISITED_VIEWS: (state, view) => {
        var alreadyAdd = false
        if (state.visitedViews.some(v => v.fullPath === view.fullPath)) return
        for (let i = 0; i < state.visitedViews.length; i++) {
          if (state.visitedViews[i].path === view.path) {
            let tempItem = Object.assign({}, view, {title: view.meta.title || 'no-name'})
            state.visitedViews.splice(i, 1)
            setTimeout(() => { // 修复vue缓存组件通过变化url的query参数来刷新页面时不执行销毁钩子的bug
              state.visitedViews.splice(i, 0, tempItem)
            }, 1000)
            alreadyAdd = true
            break
          }
        }
        if (!alreadyAdd) {
          state.visitedViews.push(Object.assign({}, view, {
            title: view.meta.title || 'no-name'
          }))
        }
        if (!view.meta.noCache) {
          if (state.cachedViews.some(t => t === view.name)) {
            for (const c of state.cachedViews) {
              if (c === view.name) {
                const index = state.cachedViews.indexOf(c)
                state.cachedViews.splice(index, 1)
                setTimeout(() => { // 修复vue缓存组件通过变化url的query参数来刷新页面时不执行销毁钩子的bug
                  state.cachedViews.splice(index, 0, view.name)
                }, 1000)
                break
              }
            }
          } else {
            state.cachedViews.push(view.name)
          }
        }
      },
      DEL_VISITED_VIEWS: (state, view) => {
        for (const [i, v] of state.visitedViews.entries()) {
          if (v.path === view.path) {
            state.visitedViews.splice(i, 1)
            break
          }
        }
        for (const i of state.cachedViews) {
          if (i === view.name) {
            const index = state.cachedViews.indexOf(i)
            state.cachedViews.splice(index, 1)
            break
          }
        }
      },
      DEL_OTHERS_VIEWS: (state, view) => {
        for (const [i, v] of state.visitedViews.entries()) {
          if (v.path === view.path) {
            state.visitedViews = state.visitedViews.slice(i, i + 1)
            break
          }
        }
        for (const i of state.cachedViews) {
          if (i === view.name) {
            const index = state.cachedViews.indexOf(i)
            state.cachedViews = state.cachedViews.slice(index, index + 1)
            break
          }
        }
      },
      DEL_ALL_VIEWS: (state) => {
        const affixTags = state.visitedViews.filter(tag => tag.meta.affix)
        state.visitedViews = affixTags
        state.cachedViews = []
      }
    },
    actions: {
      addVisitedViews({ commit }, view) {
        commit('ADD_VISITED_VIEWS', view)
      },
      delVisitedViews({ commit, state }, view) {
        return new Promise((resolve) => {
          commit('DEL_VISITED_VIEWS', view)
          resolve([...state.visitedViews])
        })
      },
      delOthersViews({ commit, state }, view) {
        return new Promise((resolve) => {
          commit('DEL_OTHERS_VIEWS', view)
          resolve([...state.visitedViews])
        })
      },
      delAllViews({ commit, state }) {
        return new Promise((resolve) => {
          commit('DEL_ALL_VIEWS')
          resolve([...state.visitedViews])
        })
      }
    }
  }
  
  export default tagsView
  